package cn.com.cpic.insurance.rulecheck.web;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.cpic.insurance.rulecheck.entity.RuleDefinitionItem;
import cn.com.cpic.insurance.rulecheck.service.RuleDefinitionItemService;

@Results({ @Result(name = CrudActionSupport.RELOAD, location = "rule-def-item.action", type = "redirect", params = {
		"ruleDefId", "${ruleDefId}" }) })
public class RuleDefItemAction extends CrudActionSupport<RuleDefinitionItem> {

	private static final long serialVersionUID = -5086505209066238266L;

	private RuleDefinitionItemService service;

	private Long id;

	private Long ruleDefId;

	private RuleDefinitionItem entity;

	private List<RuleDefinitionItem> items;

	public String execute() {
		return list();
	}

	public String list() {
		items = service.getRuleDefinitionItemList(ruleDefId);
		return SUCCESS;
	}

	public String input() {
		prepareModel();
		return INPUT;
	}

	public String save() {
		service.saveRuleDefinitionItem(this.entity);
		return RELOAD;
	}

	public String delete() {
		service.deleteRuleDefinitionItem(this.id);
		return RELOAD;
	}

	protected void prepareModel() {
		if (id != null) {
			this.entity = service.getRuleDefinitionItem(id);
		} else {
			this.entity = new RuleDefinitionItem();
			this.entity.setRuleDefinitionId(this.ruleDefId);
		}
	}

	@Autowired
	public void setService(RuleDefinitionItemService service) {
		this.service = service;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getRuleDefId() {
		return ruleDefId;
	}

	public void setRuleDefId(String ruleDefId) {
		if (StringUtils.isNotBlank(ruleDefId)) {
			this.ruleDefId = Long.parseLong(ruleDefId);
		}
	}

	public List<RuleDefinitionItem> getItems() {
		return items;
	}

	public RuleDefinitionItem getModel() {
		return entity;
	}
}
